import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jetour_line/base_widget/base_scaffold.dart';
import 'package:jetour_line/base_widget/image_network_widget.dart';
import 'package:jetour_line/base_widget/video_widget.dart';
import 'package:jetour_line/models/jetour_line_detail.dart';
import 'package:jetour_line/my_colors.dart';
import 'package:jetour_line/utils/extension_int.dart';
import 'package:video_player/video_player.dart';
class LineDetailImageScreen extends StatefulWidget {
  final List images;
  final int index;

  const LineDetailImageScreen({Key? key, required this.images,  this.index = 0})
      : super(key: key);

  @override
  State<LineDetailImageScreen> createState() => _LineDetailImageScreenState();
}

class _LineDetailImageScreenState extends State<LineDetailImageScreen> {
  PageController? _pageController;
  int selIndex = -1;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

  }

  @override
  Widget build(BuildContext context) {
    _pageController ??= PageController(initialPage: widget.index);
    if(selIndex<0){
      selIndex = widget.index;
    }
    String? title = '行程相册';
    if(widget.images[selIndex].runtimeType == Videos){
      title = '行程视频';
    }
    return BaseScaffold(
        backgroundColor: Colors.black,
        title: title,
        body: SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: Stack(
            children: [
              Padding(
                padding: EdgeInsets.only(
                  bottom: 0.px,
                ),
                child: PageView.builder(
                    controller: _pageController,
                    itemCount: widget.images.length,
                    onPageChanged: (int index) {
                      setState(() {
                        selIndex = index;
                      });
                    },
                    itemBuilder: (BuildContext context, int index) {
                      return itemWidget(index);
                    }),
              ),
              // Positioned(
              //     bottom: 10.px,
              //     right: 20.px,
              //     child: Text(
              //       '${selIndex + 1}/${widget.images.length}',
              //       style: TextStyle(fontSize: 30.px, color: MyColors.white),
              //     )),
            ],
          ),
        ));
  }


  VideoPlayerController? _controller;

  Widget itemWidget(int  index){
    if(widget.images[index].runtimeType == String){
      String img = widget.images[index];
      return GestureDetector(onTap: (){
        Navigator.pop(context);
      },child: Container(
        width: double.infinity,
        height: double.infinity,
        color: Colors.black,
        child: ImageNetworkWidget(
          img,
          width: double.infinity,
          height: double.infinity,
          fit: BoxFit.contain,
        ),
      ),);
    }else{
      Videos videos = widget.images[index];
      return VidewWidget(path: videos.url,isPlay: widget.index == index,);
    }
  }
}
